<?php


namespace app\home\controller;

use app\home\model\PgsgaYisheng as PgsgaYishengModel;
use think\Controller;
use think\Request;

class PgsgaYisheng extends Controller
{
    protected $pgsgaYishengModel;

    public function __construct(
        PgsgaYishengModel $pgsgaYishengModel
    )
    {
        parent::__construct();
        $this->pgsgaYishengModel = $pgsgaYishengModel;
    }

    public function show(Request $request)
    {
        return $this->fetch();
    }

    public function lists()
    {
        return $this->fetch();
    }

    public function ajax_lists(Request $request)
    {
        $page = $request->param('page', 1);
        $limit = $request->param('limit', 10);
        $where = [];
        $data = $this->pgsgaYishengModel->where($where)->page($page, $limit)->select();
        $count = $this->pgsgaYishengModel->where($where)->count();
        return reorganizeTableData(0, '', $count, $data);
    }

    public function info(Request $request)
    {
        $pid = $request->param('pid', '');
        $res = $this->pgsgaYishengModel->where('pid', $pid)->find();
        return jsonRes($res);
    }

    public function update(Request $request)
    {
        $pid = $request->param('pid', '');
        if(!isset($pid) || empty($pid)){
            return jsonRes([],'pid不能为空',99);
        }
        $jibing = $request->param('jibing', '');
        $zongfen = $request->param('zongfen', '');
        $panding = $request->param('panding', '');
        $fenqi = $request->param('fenqi', '0');
        $tiwen = $request->param('tiwen', '');
        $tangpijishu = $request->param('tangpijishu', '0');
        $yaowu = $request->param('yaowu', '');
        $jiliang = $request->param('jiliang', '0');
        $fare = $request->param('fare', '');
        $fare_cxsj = $request->param('fare_cxsj', '');
        $fare_time = $request->param('fare_time', '0');
        $is_jisu = $request->param('is_jisu', '0');
        $yankuang = $request->param('yankuang', '0');
        $santouji = $request->param('santouji', '0');
        $xialei = $request->param('xialei', '0');
        $zhifang = $request->param('zhifang', '0');
        $niebu = $request->param('niebu', '0');
        $suogu = $request->param('suogu', '0');
        $jianbu = $request->param('jianbu', '0');
        $gujianjirou = $request->param('gujianjirou', '0');
        $jianjia = $request->param('jianjia', '0');
        $datui = $request->param('datui', '0');
        $xiaotui = $request->param('xiaotui', '0');
        $jirou = $request->param('jirou', '0');
        $huaishuizhong = $request->param('huaishuizhong', '0');
        $dishuizhong = $request->param('dishuizhong', '0');
        $fushui = $request->param('fushui', '0');
        $tishui = $request->param('tishui', '0');
        $dingxing = $request->param('dingxing', '0');
        $pingfen = $request->param('pingfen', '');
        $data = [
            'pid' => $pid,
            'jibing' => $jibing,
            'zongfen' => $zongfen,
            'panding' => $panding,
            'fenqi' => $fenqi,
            'tiwen' => $tiwen,
            'tangpijishu' => $tangpijishu,
            'yaowu' => $yaowu,
            'jiliang' => $jiliang,
            'fare' => $fare,
            'fare_cxsj' => $fare_cxsj,
            'fare_time' => $fare_time,
            'is_jisu' => $is_jisu,
            'yankuang' => $yankuang,
            'santouji' => $santouji,
            'xialei' => $xialei,
            'zhifang' => $zhifang,
            'niebu' => $niebu,
            'suogu' => $suogu,
            'jianbu' => $jianbu,
            'gujianjirou' => $gujianjirou,
            'jianjia' => $jianjia,
            'datui' => $datui,
            'xiaotui' => $xiaotui,
            'jirou' => $jirou,
            'huaishuizhong' => $huaishuizhong,
            'dishuizhong' => $dishuizhong,
            'fushui' => $fushui,
            'tishui' => $tishui,
            'dingxing' => $dingxing,
            'pingfen' => $pingfen
        ];

        //判断数据是否存在
        $info = $this->pgsgaYishengModel->where('pid', $pid)->with(['huanzhe' => function ($query) {
            $query->field('id,hospital_id,keshi,name,gender,birthday,created');
        }])->findOrEmpty()->toArray();
        if(empty($info)){
            $res = $this->pgsgaYishengModel->save($data);
        }else{
            $res = $this->pgsgaYishengModel->save($data,['pid' => $info['pid']]);
        }

        if ($res) {
            $resBack = jsonRes($res);
        } else {
            $resBack = jsonRes($res, '更新失败', 99);
        }
        return $resBack;
    }
}